package com.bdqn.vueboot.controller;


import com.bdqn.vueboot.config.BascText;
import com.bdqn.vueboot.entity.SysUser;
import com.bdqn.vueboot.service.SysUserService;
import com.bdqn.vueboot.util.JwtTokenUtil;
import com.bdqn.vueboot.util.LoginState;
import io.swagger.annotations.*;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.naming.AuthenticationException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@RestController
@Api
//注解如果写到类上默认该类的所有方法都会添加该注解
//@CrossOrigin
public class LoginController {

    @Resource
    SysUserService userService;

    @Resource
    StringRedisTemplate stringRedisTemplate;

    /**
     * 原来登陆返回 -1、 0 、1
     *
     * @param
     * @return
     */
//    @RequestMapping(value = "/dologin")
//    @CrossOrigin
////    public String dologin(@RequestParam(value = "usrName") String usrName,
////                          @RequestParam(value = "usrPassword") String usrPassword,
////                          HttpServletResponse response) {
//    public String dologin(@RequestBody SysUser loginUser,
//                          HttpServletResponse response) {
//        //设置相应头token属性,否则response.setHeader("token", token);无法有效
//        response.setHeader("Access-Control-Expose-Headers", "token");
//        SysUser user = userService.findOneByUsrNameBiz(loginUser.getUsrName());
//        if (user == null) {
//            return LoginState.NOTEXIST;
//        } else {
//            if (!user.getUsrPassword().equals(loginUser.getUsrPassword())) {
//                return LoginState.PASSWORDWERROR;
//            } else {
//                String uuid = UUID.randomUUID().toString();
//                //System.out.println("uuid:" + uuid);
//                try {
//                    String token = JwtTokenUtil.createSign(user.getUsrName());
//                    //在执行完登录操作后,会讲token放到response响应头中讲token返回给客户端
//                    response.setHeader("token", token);
//                    BascText.setCarId(user.getUsrId());
//                } catch (Exception e) {
//                    e.printStackTrace();
//                    return LoginState.PASSWORDWERROR;
//                }
//                stringRedisTemplate.opsForValue().set(uuid, user.getUsrName(), 600, TimeUnit.SECONDS);
//                return LoginState.SUCCESS;
//            }
//        }
//    }
    @RequestMapping(value = "/dologin")
    @CrossOrigin
    public Map dologin(@RequestBody SysUser loginUser,
                       HttpServletResponse response) throws AuthenticationException {
        //设置相应头token属性,否则response.setHeader("token", token);无法有效
        response.setHeader("Access-Control-Expose-Headers", "token");
        SysUser user = userService.findOneByUsrNameBiz(loginUser.getUsrName());
        Map map = new HashMap();
        if (user == null) {
            map.put("NOTEXIST", "用户名不存在");
            return map;
        } else {
            if (!user.getUsrPassword().equals(loginUser.getUsrPassword())) {
                map.put("PASSWORDWERROR", "用户名密码不正确");
                return map;
            } else {
                String uuid = UUID.randomUUID().toString();
                //System.out.println("uuid:" + uuid);
                try {
                    String token = JwtTokenUtil.createSign(user.getUsrName());
                    //在执行完登录操作后,会讲token放到response响应头中讲token返回给客户端
                    response.setHeader("token", token);
                    BascText.setCarId(user.getUsrId());
                } catch (Exception e) {
                    e.printStackTrace();
                    throw new AuthenticationException("密码错误");
                }
                stringRedisTemplate.opsForValue().set(uuid, user.getUsrName(), 600, TimeUnit.SECONDS);
                map.put("user", user);
                return map;
            }
        }
    }

    @RequestMapping(value = "/logout")
    @CrossOrigin
    public Object logout(HttpServletRequest request) {
        String token = request.getHeader("token");
        Boolean bool = stringRedisTemplate.delete(token);
        return bool;
    }


    @ApiOperation(value = "登录操作测试")
    @PostMapping(value = "/dologintest")
    @CrossOrigin
    public String dologintest(
            @ApiParam(name = "loginUser", value = "用户实体", required = true)
            @RequestBody SysUser loginUser,
            HttpServletResponse response) {
        //解决post请求的异常
    /*public String dologin(@RequestBody Map map,
                          HttpServletResponse response) {*/
        response.setHeader("Access-Control-Expose-Headers", "token");
        SysUser user = userService.findOneByUsrNameBiz(loginUser.getUsrName());
        if (user == null) {
            return LoginState.NOTEXIST;
        } else {
            if (!user.getUsrPassword().equals(loginUser.getUsrPassword())) {
                return LoginState.PASSWORDWERROR;
            } else {
                String uuid = UUID.randomUUID().toString();
                System.out.println("uuid:" + uuid);
                response.setHeader("token", uuid);
                stringRedisTemplate.opsForValue().set(uuid, user.getUsrName(), 600);
                return LoginState.SUCCESS;
            }
        }
    }
}
